
import unittest
from primitiveBase import *

class Test(unittest.TestCase):


    def testSizer(self):
        #test conversion from base classes
        w16 = word16(1, 2, '<', name="test", format="binary", signed=False)
        s = sizer(w16)
        self.assertEqual(s.originalValue, 1)
        self.assertEqual(s.streamOffset, 2)
        self.assertEqual(s.endian, '<')
        self.assertEqual(s.name, "test")
        self.assertEqual(s.format, "binary")
        self.assertEqual(s.signed, False)
        #print s.originalValue, s.streamOffset, s.endian, s.name, s.format, s.signed
        
        w32 = word32(1, 2, '<', name="test", format="binary", signed=False)
        s = sizer(w32)
        self.assertEqual(s.originalValue, 1)
        self.assertEqual(s.streamOffset, 2)
        self.assertEqual(s.endian, '<')
        self.assertEqual(s.name, "test")
        self.assertEqual(s.format, "binary")
        self.assertEqual(s.signed, False)
        #print s.originalValue, s.streamOffset, s.endian, s.name, s.format, s.signed
        
        w64 = word64(1, 2, '<', name="test", format="binary", signed=False)
        s = sizer(w64)
        self.assertEqual(s.originalValue, 1)
        self.assertEqual(s.streamOffset, 2)
        self.assertEqual(s.endian, '<')
        self.assertEqual(s.name, "test")
        self.assertEqual(s.format, "binary")
        self.assertEqual(s.signed, False)
        #print s.originalValue, s.streamOffset, s.endian, s.name, s.format, s.signed
        
        
    def testGetSulleyString(self):
        format = """%s(1, endian="<", format="binary", signed=False, name="test")\n"""
        w16 = word16(1, 2, '<', name="test", format="binary", signed=False)
        s = w16.getSulleyString()
        expected = format % "s_word"
        self.assertEqual(s, expected)
        
        w32 = word32(1, 2, '<', name="test", format="binary", signed=False)
        s = w32.getSulleyString()
        expected = format % "s_int"
        self.assertEquals(s, expected)
        
        w64 = word64(1, 2, '<', name="test", format="binary", signed=False)
        s = w64.getSulleyString()
        expected = format % "s_double"
        self.assertEqual(s, expected)
        
        size = sizer(w64)
        size.setBlockName("blockname")
        s = size.getSulleyString()
        self.assertEquals(s, """s_size("blockname", length=8, endian="<", format="binary", signed=False, name="test")""")

    def testBuildAllStatics(self):
        fuzzLocations= set([1,2,3,4,8,9,11,12])
        inputBuff = "0123456789ABCDEFG"
        statics = static.buildAllStatics(fuzzLocations, inputBuff)
        self.assertEqual(len(statics), 4)
    
    def testCmp(self):
        testBlockStart = block("testblock", 0, True)
        testBlockStart2 = block("testblock2", 0, True)
        testBlockEnd = block("testblock", 4, False)
        testInt = word16(streamOffset=0)
        
        self.assertEqual(cmp(testBlockStart, testInt), -1)
        self.assertEqual(cmp(testInt, testBlockStart), 1)
        self.assertEqual(cmp(testBlockStart, testBlockStart2), 0)
        self.assertEqual(cmp(testBlockEnd, testInt), 1)
        self.assertEqual(cmp(testInt, testBlockEnd), -1)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()